#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("Server");
    server = new QTcpServer(this);
    server->listen(QHostAddress::Any, 8080);
    connect(server, &QTcpServer::newConnection, this, &Widget::new_connect);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_bt_enter_clicked()
{
    QString str = ui->send_mess->text();
    socket->write(str.toUtf8());
}

void Widget::new_connect()
{
    //获取套接字
    socket = server->nextPendingConnection();
    ui->lb_conn->setText("连接成功");
    connect(socket, &QTcpSocket::readyRead, this ,[=](){
        QString data = socket->readAll();
        ui->receive_mess->setText(data);
    });
    connect(socket, &QTcpSocket::disconnected, this, [=](){
        ui->lb_conn->setText("无连接");
        socket->close();
        socket->deleteLater();
    });
}
